package com.atguigu.tingshu.user.pattern.impl;


import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.pattern.DeliveryStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component(SystemConstant.ORDER_ITEM_TYPE_ALBUM)
public class AlbumDelivery implements DeliveryStrategy {


    @Autowired
    private UserPaidAlbumMapper userPaidAlbumMapper;

    @Override
    public void delivery(UserPaidRecordVo userPaidRecordVo) {

        //根据type来判断用户买的是那种商品,是VIP还是专辑,还是声音

        Long userId = userPaidRecordVo.getUserId();

        String itemType = userPaidRecordVo.getItemType();

        String orderNo = userPaidRecordVo.getOrderNo();

        List<Long> itemIdList = userPaidRecordVo.getItemIdList();
        //走到这说明买的是专辑
        //判断是否购买过专辑,购买过直接忽略
        Long count = userPaidAlbumMapper.selectCount(
                new LambdaQueryWrapper<UserPaidAlbum>()
                        .eq(UserPaidAlbum::getOrderNo, orderNo)
                        .select(UserPaidAlbum::getId)
        );

        if (count > 0){
            return;
        }

        //走到这说明没处理过
        //保存声音购买记录
        UserPaidAlbum userPaidAlbum = new UserPaidAlbum();
        userPaidAlbum.setUserId(userId);
        userPaidAlbum.setOrderNo(orderNo);
        userPaidAlbum.setAlbumId(itemIdList.get(0));
        userPaidAlbumMapper.insert(userPaidAlbum);

    }
}
